<?php foreach ($program as $key): ?>
		<div class="ibox float-e-margins">
			<div class="ibox-title" style="border: none;">
				<h5 style="color: #00afef"><i>Edit</i></h5>
				<div class="ibox-tools">
					<button class="btn btn-sm btn-primary save-btn-tab" id="saveEditForm">Save</button>
					<button class="btn btn-sm btn-primary btn-outline close-edit cancel-btn-tab">Cancel</button>
				</div>
			</div>
			<div class="ibox-content">
				<form class="form-horizontal" id="form-forms-edit">
					<div class="form-group">
						<label class="col-lg-4 control-label">Code</label>
						<input type="hidden" value="<?php echo $key->program_id; ?>" name="pid">
						<div class="col-lg-3">
							<input readonly value="<?php echo $key->program_code; ?>" name="pcode" type="text" class="form-control">
						</div>
					</div>
					<div class="form-group">
						<label class="col-lg-4 control-label">Name</label>
						<div class="col-lg-5">
							<input value="<?php echo $key->program_name; ?>" type="text" name="pname" class="form-control">
						</div>
					</div>
					<div class="form-group">
						<label class="col-lg-4 control-label">Description</label>
						<div class="col-lg-5">
							<input type="text" value="<?php echo $key->description; ?>" name="desc" class="form-control">
						</div>
					</div>
					<div class="form-group">
                        <label class="col-lg-4 control-label">Program Type</label>
                        <div class="col-lg-5">
                            <select class="form-control m-b chosen" name="pType" id ="prog_type_e">
                                <option value="" id="empty-option">Select Program</option>
                                 <?php foreach($prog_type as $p): ?>
                                <option <?php echo ($key->prog_type == $p->lookup_code ? 'selected' : ''); ?> value="<?php echo $p->lookup_code; ?>"><?php echo $p->meaning; ?></option>
                                <?php endforeach ?>
                            </select>
                        </div>
                    </div>
					<div class="form-group">
						<label class="col-lg-4 control-label">Module</label>
						<div class="col-lg-5">
							<input type="hidden" name="module_id_hidden" value="<?php echo $key->module_id; ?>">
							<select class="form-control m-b chosen" name="modID" id ="module_option_e">
								<option value="" id="empty-option">Select Module</option>
								<?php foreach ($mods as $m) {?>
								<option value ="<?php echo $m->module_id; ?>" <?php echo ($key->module_id == $m->module_id) ? 'selected' : ''; ?>><?php echo $m->module_name; ?></option>
								<?php }
?>
							</select>
						</div>
					</div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Module Menu</label>
                        <input type="hidden" id="mod_menu_id_hidden" value="<?php echo $key->group_id; ?>">
                        <div class="col-lg-5">
                            <select class="form-control m-b chosen" name="ppGroup" id ="module_menu_option_e">
                                <option value="" id="empty-option">Select Module Menu</option>
                            </select>
                        </div>
                    </div>
					<div class="form-group">
						<label class="col-lg-4 control-label">Executable Filename</label>
						<div class="col-lg-5">
							<input type="text" name="exename" value="<?php echo $key->exe_file_name; ?>" class="form-control">
						</div>
					</div>
					<div class="form-group">
						<label class="col-lg-4 control-label">Enable</label>
						<div class="col-lg-1">
							<input <?php echo ($key->enable_flag == 'Y' ? 'checked' : ''); ?> type="checkbox" class="i-checks" value="ok" name="enabled">
						</div>
					</div>
					</form>
				</div>
                <div class="ibox-content">
                    <form id="prog_param">
                        <table class="table dataTables table-bordered">
                            <thead>
                                <tr>
                                    <th style="width: 80px">Seq. No.</th>
                                    <th>Name</th>
                                    <th>Description</th>
                                    <th>Data Type</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <input type="hidden" value="<?php echo $key->program_id; ?>" name="program_id">
                                    <td style="margin:0px;padding:0px;">
                                        <input name="seq_no" type="text" class="form-control-custom">
                                    </td>
                                    <td style="margin:0px;padding:0px;">
                                        <input name="name" type="text" class="form-control-custom">
                                    </td>
                                    <td style="margin:0px;padding:0px;">
                                        <input name="desc" type="text" class="form-control-custom">
                                    </td>
                                    <td style="margin:0px;padding:0px;">
                                        <select name="data_type" class="form-control chosen-select" name="" id="">
                                            <option value="">--</option>
                                            <?php foreach($data_type as $d): ?>
                                            <option value="<?php echo $d->lookup_code; ?>"><?php echo $d->meaning; ?></option>
                                        <?php endforeach; ?>
                                        </select>
                                    </td>
                                    <td style="margin:0px;padding:0px;">
                                        <button type="button" style="width:100%;height: 29px;" class="btn btn-white btn-md" id="saveparam"><i class="fa fa-plus success "></i></button>
                                    </td>
                                </tr>
                            </tbody>
                            <tbody id="prog_param_tbl">
                                
                            </tbody>
                        </table>
                    </form>
                </div>
			</div>
		</div>
	</div>
    <div class="modal fade" id="editparam_modal" tabindex="-1" role="dialog">
      <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
               <h4 class="modal-title" id="myModalLabel">Edit Program Params</h4>
            </div>
          <div class="modal-body">
            <div class="row">
              <form id="paramsedit_form" method="post" class="form-horizontal">
                  <input type="hidden" name="param_id" value="">
                  <div class="form-group">
                      <label class="col-lg-2 control-label">Seq. No.</label>
                      <div class="col-lg-4">
                          <input type="text" name="seq_no" class="form-control" value="">
                      </div>
                       <label class="col-lg-2 control-label">Name</label>
                      <div class="col-lg-4">
                          <input type="text" name="name" class="form-control" value="">
                      </div>
                  </div>
                  <div class="form-group">
                      <label class="col-lg-2 control-label">Description</label>
                      <div class="col-lg-4">
                          <input type="text" name="desc" class="form-control" value="">
                      </div>
                       <label class="col-lg-2 control-label">Data Type</label>
                      <div class="col-lg-4">
                          <select name="data_type" class="form-control chosen-select" name="" id="">
                            <option value="">--</option>
                            <?php foreach($data_type as $d): ?>
                            <option value="<?php echo $d->lookup_code; ?>"><?php echo $d->meaning; ?></option>
                        <?php endforeach; ?>
                        </select>
                      </div>
                  </div>
              </form>
            </div>
          </div>
          <div class="modal-footer">
               <button id="saveeditparams" type="button" class="btn btn-primary" >Save</button>
          </div>
        </div>
      </div>
    </div> <!-- end-of-modal -->
	<?php endforeach;?>
	<script type="text/javascript">
	$(document).ready(function() {
		$('.chosen').chosen({width: '100%'});
        $(".chosen-select").chosen({disable_search_threshold: 10,width: '100%'});
        getparams($('#prog_param input[name=program_id]').val());
		$.ajax({
	        type: "POST",
	        url: "<?php echo base_url($this->session->userdata('forajax') . '/searchmodmenu'); ?>",
	        data: { "mod_menu_id" :  $('#module_option_e').val() },
	        dataType: 'json',
	        success: function(e){
	           console.log(e);
                var tmp = "";
                var id = $('#mod_menu_id_hidden').val();
                $.each(e, function(i, item) {
                	var selected = '';
                	if(e[i].menu_id == id){
                		selected = "selected";
                	}
                    tmp += "<option value='"+e[i].menu_id+"' "+selected+">"+e[i].menu_name+"</option>";
                });
                tmp = tmp + "<option value='-1'>None</option>";
                 $('#module_menu_option_e').html(tmp);
                 $('select').trigger('chosen:updated');
	        }
        });

        function getparams(id){
            $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax') . '/getparams'); ?>",
                data: { "id" :  id },
                dataType: 'json',
                success: function(e){
                    var tmp = '';
                    $.each(e, function(i, item) {
                        tmp += "<tr><td>"+ e[i].seq_no +"</td><td>"+ e[i].name +"</td><td>"+ e[i].desc +"</td><td>"+ e[i].data_type +'</td><td align="center"><button type="button" progid="'+ e[i].prog_param_id +'" class="btn btn-sm btn-primary btn-xs btn-outline edit-param" title="Edit"><i class="fa fa-pencil-square-o fa-lg"></i></button><button type="button" progid="'+ e[i].prog_param_id +'" class="btn btn-sm btn-danger btn-xs btn-outline delete-param" title="Edit"><i class="fa fa-trash-o fa-lg"></i></button></td></tr>';
                    });
                    $('#prog_param_tbl').html(tmp);
                   //console.log(e);
                }
            });
        }

        $(document).on('click','.edit-param',function(){
            var id = $(this).attr('progid');
            $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax') . '/getparamsbyid'); ?>",
                data: { "id" :  id },
                dataType: 'json',
                success: function(e){
                    $('#paramsedit_form input[name=seq_no]').val(e[0].seq_no);
                    $('#paramsedit_form input[name=name]').val(e[0].name);
                    $('#paramsedit_form input[name=desc]').val(e[0].desc);
                    $('#paramsedit_form select[name=data_type]').val(e[0].data_type);
                    $('#paramsedit_form input[name=param_id]').val(e[0].prog_param_id);
                    $(".chosen-select").trigger('chosen:updated');
                }
            });
            $('#editparam_modal').modal('show');
        });

        $(document).on('click','.delete-param',function(){
            var id = $(this).attr('progid');
            swal({
                title: "Are you sure?",
                text: "Data will be Lost!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Yes, delete it!",
                cancelButtonText: "No, cancel please!",
                closeOnConfirm: true,
                closeOnCancel: true },
            function (isConfirm) {
                if (isConfirm) {
                    swal("Deleted!", "Data is unrecovable", "success");
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/deleteparambyid'); ?>",
                        data: { "id" :  id },
                        async: true,
                        dataType: 'json',
                        success: function(e){
                            getparams($('#form-forms-edit input[name=pid]').val());
                        }
                    });
                    
                } else {
                    swal("Cancelled",  "Action aborted", "error");
                }
            });
        });

		$('#module_option_e').change(function(){
            var id = $('#module_option_e').val();
            console.log(id);
            $.ajax({
            type: "POST",
            url: "<?php echo base_url($this->session->userdata('forajax') . '/searchmodmenu'); ?>",
            data: { "mod_menu_id" : id },
            dataType: 'json',
            success: function(e){
                console.log(e);
                var tmp = "";
                var id = $('#mod_menu_id_hidden').val();
                $.each(e, function(i, item) {
                	var selected = '';
                	if(e[i].menu_id == id){
                		selected = "selected";
                	}
                    tmp += "<option value='"+e[i].menu_id+"' "+selected+">"+e[i].menu_name+"</option>";
                });
                tmp = tmp + "<option value='-1'>None</option>";
                 $('#module_menu_option_e').html(tmp);
                 $('select').trigger('chosen:updated');
            }
            });
        });

		$('.i-checks').iCheck({
	        checkboxClass: 'icheckbox_square-green',
	        radioClass: 'iradio_square-green'
	    });
	    $("#form-forms-edit").validate({
                 rules: {
                     pcode: {
                        required: true
                     },
                     pname: {
                        required : true
                     },
                     modID: {
                        required: true
                     },
                     ppGroup: {
                        required: true
                     },
                     pType: {
                        required: true
                     },
                     exename: {
                        required: true
                     }
                 }
        });

        $("#prog_param").validate({
            rules: {
                seq_no: {
                   required: true
                },
                name: {
                   required : true
                },
                desc: {
                   required: true
                },
                data_type: {
                   required: true
                }
            }
        });

        $("#paramsedit_form").validate({
            rules: {
                seq_no: {
                   required: true
                },
                name: {
                   required : true
                },
                desc: {
                   required: true
                },
                data_type: {
                   required: true
                }
            }
        });

        $('#saveeditparams').click(function(){
            if($("#paramsedit_form" ).valid() == true){
                $.ajax({
                    url: "<?php echo base_url($this->session->userdata('forajax') . '/saveparamedit'); ?>",
                    method:'post',
                    data:$('#paramsedit_form').serialize(),
                    dataType:'json',
                    success: function (data) {
                        if(data.err == '1'){
                            notifymsg('danger','<b>Error:</b> '+data.msg);
                        }else{
                            notifymsg('success','<b>Success</b>: Data saved.');
                            getparams($('#form-forms-edit input[name=pid]').val());
                        }
                    },
                    complete: function() {
                        $("#saveeditparams").removeAttr("disabled");
                    },
                    beforeSend: function(){
                        $("#saveeditparams").attr("disabled", true);
                    }
                });
            }
        });

        $('#saveparam').click(function(){
            if($("#prog_param" ).valid() == true){
                $.ajax({
                    url: "<?php echo base_url($this->session->userdata('forajax') . '/saveparam'); ?>",
                    method:'post',
                    data:$('#prog_param').serialize(),
                    dataType:'json',
                    success: function (data) {
                        if(data.err == '1'){
                            notifymsg('danger','<b>Error:</b> '+data.msg);
                        }else{
                            notifymsg('success','<b>Success</b>: Data saved.');
                            getparams($('#prog_param input[name=program_id]').val());
                        }
                    },
                    complete: function() {
                        $("#saveparam").removeAttr("disabled");
                    },
                    beforeSend: function(){
                        $("#saveparam").attr("disabled", true);
                    }
                });
                $('#editparam_modal').modal('hide');
            }
        });

        $('#saveEditForm').click(function(){
        	if($("#form-forms-edit" ).valid() == true){
            $.ajax({
                url: "<?php echo base_url($this->session->userdata('forajax') . '/update'); ?>",
                method:'post',
                data:$('#form-forms-edit').serialize(),
                dataType:'json',
                success: function (data) {
                    var text = data.msg;
                    var term = "exists";
                    console.log(text);
                    if(data.err == '1'){
                        notifymsg('danger','<b>Error:</b> '+data.msg);
                    }else if(text === null){
                            $('#add-link').remove();
                            $('#add').remove();
                            $('#tab-list a:first').tab('show');
                            $('#count_add').val('0');
                            notifymsg('success','<b>Success</b>: Data saved.');
                    }else if(text.indexOf(term) !== -1){
                            $('#err-msg').html(data.msg);
                            notifymsg('danger','<b>Error</b>: '+data.msg);
                    }else{

                    }
                },
                complete: function() {
                    $("#saveForm").removeAttr("disabled");
                },
                beforeSend: function(){
                    $("#saveForm").attr("disabled", true);
                }
            });
			}
		});
	});
	</script>